package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.woniu_bx.exception.GoodsCategoryExistException;
import com.woniu.woniu_bx.exception.HelperCateNameIsExistException;
import com.woniu.woniu_bx.mapper.DiscussMapper;
import com.woniu.woniu_bx.mapper.GoodsCategoryMapper;
import com.woniu.woniu_bx.pojo.*;
import com.woniu.woniu_bx.service.DiscussService;
import com.woniu.woniu_bx.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: Ayang
 * @Date: 2022/8/27 17:45
 * @Description: 商品类别service实现类
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {
    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public boolean add(GoodsCategory goodsCategory) {
        if (goodsCategoryMapper.selectNum(goodsCategory.getName()) == 1){
            throw new GoodsCategoryExistException("该商品类别名已存在");
        }
        goodsCategoryMapper.insert(goodsCategory);
        return true;
    }

    @Override
    public boolean update(GoodsCategory goodsCategory) {
        String name = goodsCategory.getName();
        GoodsCategory oldgoodsCategory = goodsCategoryMapper.selectById(goodsCategory.getId());
        if(!oldgoodsCategory.getName().equals(goodsCategory.getName())){
            Integer num = goodsCategoryMapper.selectNum(name);
            if(num==0){
                int i = goodsCategoryMapper.updateById(goodsCategory);
                return true;
            }else{
                throw new GoodsCategoryExistException("该商品类别名已存在");
            }
        }
        int i = goodsCategoryMapper.updateById(goodsCategory);
        return true;
    }

    @Override
    public PageBean<GoodsCategory> getByPage(int pageNum,int sizeNum) {
        PageBean<GoodsCategory> pageBean= new PageBean<>();
        Page<GoodsCategory> p = PageHelper.startPage(pageNum,sizeNum) ;
        List<GoodsCategory> data = goodsCategoryMapper.selectByOrderNumber();
        pageBean.setData(data);
        pageBean.setCurrPage(pageNum);
        pageBean.setCurrPageSize(data.size());
        pageBean.setTotalNums((int)p.getTotal());
        pageBean.setTotalPages(p.getPages());
        pageBean.setPageSize(sizeNum);
        return pageBean ;
    }

    @Override
    public boolean deleteById(int id) {
        int i = goodsCategoryMapper.deleteById(id);
        if(i==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<GoodsCategory> selectByName(String name,String nav,String status) {
        return goodsCategoryMapper.selectByName(name,nav,status);
    }

    @Override
    public void lock(int id) {
        goodsCategoryMapper.lock(id);
    }

    @Override
    public void unlock(int id) {
        goodsCategoryMapper.unlock(id);
    }

    @Override
    public void changeorderNumber(int id1, int id2) {
        goodsCategoryMapper.changeorderNumber(id1,id2);
    }

    @Override
    public GoodsCategory findByOrderNumber(int orderNumber) {
        return goodsCategoryMapper.findByOrderNumber(orderNumber);
    }

    @Override
    public GoodsCategory findByName(String name) {
        return goodsCategoryMapper.findByName(name);
    }

    @Override
//    @Cacheable(cacheNames = "goodsCategories")
    public List<GoodsCategory> selectByOrderNumberReception() {
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByOrderNumberReception();
        return goodsCategories;
    }

    @Override
    public List<Goods> findByCategoryId(int id) {
        return goodsCategoryMapper.findByCategoryId(id);
    }
}
